﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using transportsystem.DTO;
using transportsystem.BUS;

namespace transportsystem
{
    public partial class frmChuyen : Form
    {
        DataTable dt1,dt2;
        bool hideControls = true;
        int iIndex = 0;
        bool addNew = false;

        void HideAddNew(bool value)
        {
            if (value == true)
                pnlAddNew.Visible = false;
            else
                pnlAddNew.Visible = true;
        }
        public frmChuyen()
        {
            InitializeComponent();
        }

        public void SetDataGridTitle1()
        {
            string[] a = new string[3] { "Mã chuyến", "Giờ khởi hành", "Mã tuyến" };

            for (int i = 0; i < a.Count(); i++)
                dgv1.Columns[i].HeaderText = a[i];
        }
        public void SetDataGridTitle2()
        {
            string[] a = new string[3] { "Mã chuyến", "Giờ khởi hành", "Mã tuyến" };

            for (int i = 0; i < a.Count(); i++)
                dgv1.Columns[i].HeaderText = a[i];
        }

        private void chkHideControls_CheckedChanged(object sender, EventArgs e)
        {
            hideControls = !hideControls;
            HideAddNew(hideControls);
        }

        private void frmChuyen_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        public void LoadData()
        {
            HideAddNew(hideControls);
            dt1 = ChuyenBUS.layDanhSachChuyen();
            if (dt1 != null && dt1.Rows.Count > 0)
                dgv1.DataSource = dt1;
            iIndex = 0;
            
            SetValueToControls();

            dt2 = ChuyenBUS.layDanhSachChuyen();
            if (dt2 != null && dt2.Rows.Count > 0)
                dgv2.DataSource = dt2;
            SetValueToControls2();
        }

        private void SetValueToControls2()
        {
            if (dt2.Rows.Count > 0)
            {
                txtMaChuyen2.Text = dt2.Rows[iIndex]["machuyen"].ToString().Trim();
                txtGioKhoiHang2.Text = dt2.Rows[iIndex]["giokhoihanh"].ToString().Trim();
                layDanhSachTuyen(dt2, cbTuyen2);                
            }
        }
        public void SetValueToControls()
        {
            if (dt1.Rows.Count > 0)
            {
                txtMaChuyen1.Text = dt1.Rows[iIndex]["machuyen"].ToString().Trim();
                txtGioKhoiHang1.Text = dt1.Rows[iIndex]["giokhoihanh"].ToString().Trim();
                layDanhSachTuyen(dt1, cbTuyen1);                
            }
        }

        public void layDanhSachTuyen(DataTable dt, ComboBox cb)
        {
            DataTable dtTuyen = TuyenBUS.layDanhSachTuyen();
            if (dtTuyen != null && dtTuyen.Rows.Count > 0)
            {
                cb.DataSource = dtTuyen;                
                cb.SelectedValue = dt.Rows[iIndex]["matuyen"].ToString();
            }
        }
        
        private void dgv1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            addNew = false;
            iIndex = e.RowIndex;
            SetValueToControls();
        }

        private void dgv2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            addNew = false;
            iIndex = e.RowIndex;
            SetValueToControls();
        }

        private void dgv1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            addNew = false;
            iIndex = e.RowIndex;
            SetValueToControls();
        }

        private void dgv2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            addNew = false;
            iIndex = e.RowIndex;
            SetValueToControls2();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }
        public void Save()
        {
            Chuyen c1 = new Chuyen();
            c1.Machuyen = int.Parse(txtMaChuyen1.Text);
            c1.Giokhoihanh = Convert.ToDateTime(txtGioKhoiHang1.Text);
            c1.Matuyen = Convert.ToInt32(cbTuyen1.SelectedValue);

            Chuyen c2 = new Chuyen();
            c2.Machuyen = int.Parse(txtMaChuyen2.Text);
            c2.Giokhoihanh = Convert.ToDateTime(txtGioKhoiHang2.Text);
            c2.Matuyen = Convert.ToInt32(cbTuyen2.SelectedValue);

            bool kq = ChuyenBUS.capNhatChuyen(c1,c2);
            if (kq == true)
                MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("Có lỗi xảy ra! Vui lòng thử lại", "Thông báo", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
            LoadData();
            addNew = false;

        }
    }
}
